package com.szxy.hotel.controller;

import com.szxy.hotel.entity.Client;
import com.szxy.hotel.entity.Hotelroom;
import com.szxy.hotel.entity.PageBean;
import com.szxy.hotel.entity.Room;
import com.szxy.hotel.service.ClientService;
import com.szxy.hotel.service.ReserveService;
import com.szxy.hotel.util.App;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ReserveController {
    @Resource(name = "reserveService")
    ReserveService reserveService;
    @Resource(name = "clientService")
    ClientService clientService;
    /**
     * 增加预定的房间类型对应的房间号
     */
    @RequestMapping("/selectAddType.do")
    @ResponseBody
    public Map<String,Object> selectAddType(String roomtype){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Hotelroom> list = reserveService.selectAddType(roomtype);
        if(list.size()<=0){
            map.put("status",1);
            map.put("message","没有数据");
            map.put("result",null);
        }else {
            map.put("status",0);
            map.put("message","查询成功");
            map.put("result",list);
        }
        return map;
    }

    /**
     * 修改预定的房间类型对应的房间号
     */
    @RequestMapping("/selectUpdateType.do")
    @ResponseBody
    public Map<String,Object> selectUpdateType(String roomtype1){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Hotelroom> list = reserveService.selectAddType(roomtype1);
        if(list.size()<=0){
            map.put("status",1);
            map.put("message","没有数据");
            map.put("result",null);
        }else {
            map.put("status",0);
            map.put("message","查询成功");
            map.put("result",list);
        }
        return map;
    }

    /**
     * 删除预定的房间类型对应的房间号
     */
    @RequestMapping("/selectDeleteType.do")
    @ResponseBody
    public Map<String,Object> selectDeleteType(String roomtype2){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Hotelroom> list = reserveService.selectAddType(roomtype2);
        if(list.size()<=0){
            map.put("status",1);
            map.put("message","没有数据");
            map.put("result",null);
        }else {
            map.put("status",0);
            map.put("message","查询成功");
            map.put("result",list);
        }
        return map;
    }
    /**
     * 增加预定
     */
    @RequestMapping("/addReserve.do")
    public String addReserve(Room room, Client client){
        Map<String, Object> map = new HashMap<String, Object>();
        Client c = clientService.findByclientname(client.getClientName());
        Hotelroom h = reserveService.findByhotelroomid(room.getRoomId());
        Double hrPrice = h.getHotelPrice();
        Room room1 = reserveService.findByroomid(room.getRoomId());
        if(room1 !=null&& room1.getRoomState().equals("已预订")||room1 !=null&&room1.getRoomState().equals("已入住")){
            map.put("status",1);
            map.put("message","非空房不能预订");
        }else {
            if(c==null){
                map.put("status",1);
                map.put("message","客户名不存在");
            }else if(c.getClientPhone().equals(client.getClientPhone())&&c.getClientIdentity().equals(client.getClientIdentity())){
                Double money = c.getClientMoney();
                if(room.getRoomReserve()==null||room.getRoomExpire()==null){
                    map.put("status",1);
                    map.put("message","日期不能为空");
                }else{
                    String roomintime = room.getRoomReserve();
                    String roomouttime = room.getRoomExpire();
                    String[] intime = roomintime.split("-");
                    String[] outtime = roomouttime.split("-");
                    int inyear = Integer.parseInt(intime[0]);
                    int inmonth = Integer.parseInt(intime[1]);
                    int indate = Integer.parseInt(intime[2]);
                    int outyear = Integer.parseInt(outtime[0]);
                    int outmonth = Integer.parseInt(outtime[1]);
                    int outdate = Integer.parseInt(outtime[2]);
                    if(inyear<outyear){
                        if(inmonth!=12||outmonth!=1){
                            map.put("status",1);
                            map.put("message","本店最多只能居住一个月，如需长期居住，需要一个月缴一次费");
                        }else {
                            if((outyear-inyear)==1){
                                int inalldate = App.day(inyear,inmonth);
                                Double m = (inalldate-indate+outdate)*hrPrice;
                                int d = inalldate-indate+outdate;
                                Map<String, Object> map1 = booleanMoney(money,client,room,m,c,hrPrice,d);
                                map = map1;
                            }else {
                                map.put("status",1);
                                map.put("message","本店最多只能居住一个月，如需长期居住，需要一个月缴一次费");
                            }
                        }
                    }else if(inyear==outyear){
                        if(inmonth<outmonth){
                            if((outmonth-inmonth)==1){
                                int inalldate = App.day(inyear,inmonth);
                                Double m = (inalldate-indate+outdate)*hrPrice;
                                int d = inalldate-indate+outdate;
                                Map<String, Object> map1 = booleanMoney(money,client,room,m,c,hrPrice,d);
                                map = map1;
                            }else {
                                map.put("status",1);
                                map.put("message","本店最多只能居住一个月，如需长期居住，需要一个月缴一次费");
                            }
                        }else if(inmonth==outmonth){
                            if(indate<outdate){
                                Double m = (outdate-indate)*hrPrice;
                                int d = outdate-indate;
                                Map<String, Object> map1 = booleanMoney(money,client,room,m,c,hrPrice,d);
                                map = map1;
                            }else if(indate==outdate){
                                map.put("status",1);
                                map.put("message","本店最少居住一天");
                            }else {
                                map.put("status",1);
                                map.put("message","请正确填写日期");
                            }
                        }else {
                            map.put("status",1);
                            map.put("message","请正确填写日期");
                        }
                    }else{
                        map.put("status",1);
                        map.put("message","请正确填写日期");
                    }
                }
            }else {
                map.put("status",1);
                map.put("message","电话号码或身份证号不正确");
            }
        }
        return "redirect:toReserve.do";
    }

    /**
     * 增加预定的相关操作
     */
    private Map<String,Object> booleanMoney(Double money, Client client, Room room, Double m, Client c,Double hrPrice,int d) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(money<client.getClientMoney()){
            map.put("status",1);
            map.put("message","余额不足请充值");
        }else {
            if(client.getClientMoney()>=m){
                money = money-m;
                room.setRoomPrice(hrPrice);
                room.setRoomState("已预订");
                room.setRoomName(c.getClientName());
                room.setRoomTime(d);
                int result = reserveService.insertRoom(room);
                if(result==1){
                    clientService.updateClient(c.getClientName(),money);
                    reserveService.updateHotelstate(room.getRoomId(),room.getRoomState());
                }else {
                    map.put("status",1);
                    map.put("message","预定失败");
                }
            }else {
                map.put("status",1);
                map.put("message","押金不足请充值");
            }
        }
        return map;
    }

    /**
     * 修改预定
     */
    @RequestMapping("/updateReserve.do")
    public String updateReserve(Room room, Client client){
        Client c = clientService.findByclientname(client.getClientName());
        Room room1 = reserveService.findByroomid(room.getRoomId());
        if(room1 !=null&& room1.getRoomState().equals("已预订")){
            if(c!=null&&c.getClientPhone().equals(client.getClientPhone())&&c.getClientIdentity().equals(client.getClientIdentity())){
                if(c.getClientName().equals(room1.getRoomName())){
                    if(room.getRoomReserve()!=null&&room.getRoomExpire()!=null){
                        String roomintime = room.getRoomReserve();
                        String roomouttime = room.getRoomExpire();
                        String[] intime = roomintime.split("-");
                        String[] outtime = roomouttime.split("-");
                        int inyear = Integer.parseInt(intime[0]);
                        int inmonth = Integer.parseInt(intime[1]);
                        int indate = Integer.parseInt(intime[2]);
                        int outyear = Integer.parseInt(outtime[0]);
                        int outmonth = Integer.parseInt(outtime[1]);
                        int outdate = Integer.parseInt(outtime[2]);
                        if(inyear<outyear){
                            if(inmonth==12&&outmonth==1){
                                if(outyear-inyear==1){
                                    int inalldate = App.day(inyear,inmonth);
                                    if((inalldate-indate+outdate)==room1.getRoomTime()){
                                        reserveService.updateDate(room);
                                    }
                                }
                            }
                        }else if(inyear==outyear){
                            if(inmonth<outmonth){
                                if((outmonth-inmonth)==1){
                                    int inalldate = App.day(inyear,inmonth);
                                    if((inalldate-indate+outdate)==room1.getRoomTime()){
                                        reserveService.updateDate(room);
                                    }
                                }
                            }else if(inmonth==outmonth){
                                if(indate<outdate){
                                    if((outdate-indate)==room1.getRoomTime()){
                                        reserveService.updateDate(room);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return "redirect:toReserve.do";
    }
    /**
     * 房间预定列表及查询预定
     */
    @RequestMapping("/roomList.do")
    @ResponseBody
    public Map<String,Object> roomList(int currentPage,String userName3,String Identity3){
        Map<String, Object> map = new HashMap<String, Object>();
        PageBean<Room> pageBean = new PageBean<Room>();
        String roomtype = "已预订";
        //一页的记录数
        int pageSize = 8;
        //下标初始值
        int start = (currentPage - 1) * pageSize;
        //要查询的记录数
        int size = pageSize;
        if((userName3!=""&&userName3!=null)&&(Identity3!=""&&Identity3!=null)){
            Client c = clientService.findByclientname(userName3);
            if(c==null){
                map.put("status",1);
                map.put("message","该用户名不存在");
            }else if(c.getClientIdentity().equals(Identity3)){
                List<Room> lists = reserveService.findByPageSearch(start, size,roomtype,userName3);
                List<Room> list = reserveService.selectCountSearch(roomtype,userName3);
                int count = list.size();
                if (count <= 0) {
                    map.put("status", 1);
                    map.put("message", "没有数据");
                    map.put("result", null);
                }else {
                    int totalCount = count;
                    double tc = totalCount;
                    //总页数
                    int totalPage = (int) Math.ceil(tc / pageSize);
                    pageBean.setCurrentage(currentPage);
                    pageBean.setPageSize(pageSize);
                    pageBean.setTotalCount(totalCount);
                    pageBean.setTotalPage(totalPage);
                    pageBean.setLists(lists);
                    map.put("status", 0);
                    map.put("message", "有数据");
                    map.put("result", pageBean);
                }
            }else {
                map.put("status",1);
                map.put("message","该用户的身份证不正确");
            }
        }else {
            //查询的所有Room对象集合
            List<Room> lists = reserveService.findByPage(start, size,roomtype);
            //Room对象的总数
            List<Room> list = reserveService.selectCount(roomtype);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "没有数据");
                map.put("result", null);
            }else {
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }
        return map;
    }
    /**
     * 删除预定
     */
    @RequestMapping("/deleteReserve.do")
    public String deleteReserve(Room room, Client client){
        Client c = clientService.findByclientname(client.getClientName());
        Room room1 = reserveService.findByroomid(room.getRoomId());
        if(c!=null&&c.getClientIdentity().equals(client.getClientIdentity())){
            if(room1 !=null&& room1.getRoomState().equals("已预订")&&c.getClientName().equals(room1.getRoomName())){
                String rs = "已退订";
                int result = reserveService.deleteReserve(room1.getRoomId(),rs);
                if(result==1){
                    Double m = room1.getRoomPrice()*room1.getRoomTime();
                    m += c.getClientMoney();
                    clientService.updateClient(c.getClientName(),m);
                    reserveService.updateHotelstate(room1.getRoomId(),rs);
                }
            }
        }
        return "redirect:toReserve.do";
    }
}
